package com.wxxymakers.cklabrecruit.service;

import com.wxxymakers.cklabrecruit.entity.EmailReturn;
import com.wxxymakers.cklabrecruit.entity.EmailReturnExample;
import com.wxxymakers.cklabrecruit.mapper.EmailReturnMapper;
import com.wxxymakers.cklabrecruit.utils.PageUtils;
import com.wxxymakers.cklabrecruit.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @AUTHOR soft
 * @DATE 2018/11/30 21:14
 * @DESCRIBE
 */
@Service
public class EmailReturnService {

    private final EmailReturnMapper mapper;

    @Autowired
    public EmailReturnService(EmailReturnMapper mapper) {
        this.mapper = mapper;
    }

    @Transactional
    public EmailReturn add(EmailReturn emailReturn) {
        if (emailReturn != null) {
            emailReturn.setDatetime(new Date());
            emailReturn.setStatus(false);
            emailReturn.setResends(0);
            if (mapper.insertSelective(emailReturn) > 0) {
                return emailReturn;
            }
        }
        return null;
    }

    @Transactional
    public boolean deleteByRecordId(Integer... recordIds) {
        for (Integer recordId : recordIds) {
            EmailReturnExample example = new EmailReturnExample();
            example.createCriteria().andRecordIdEqualTo(recordId);
            mapper.deleteByExample(example);
        }
        return true;
    }

    /**
     * 根据报名人id删除回执记录
     * @param regIds 报名人id列表
     * @return 成功与否
     */
    @Transactional
    public boolean deleteByRegId(Integer... regIds) {
        for (Integer regId : regIds) {
            EmailReturnExample example = new EmailReturnExample();
            example.createCriteria().andRegIdEqualTo(regId);
            mapper.deleteByExample(example);
        }
        return true;
    }

    /**
     * 改变反馈状态
     * @param emailReturn 邮件反馈信息
     * @return 是否成功
     */
    @Transactional
    public boolean change(EmailReturn emailReturn) {
        emailReturn.setDatetime(new Date());
        EmailReturnExample example = new EmailReturnExample();
        EmailReturnExample.Criteria criteria = example.createCriteria();
        if (!Utils.isZero(emailReturn.getId())) {
            criteria.andIdEqualTo(emailReturn.getId());
        } else if (!Utils.isZero(emailReturn.getRecordId()) && !Utils.isZero(emailReturn.getRegId())) {
            criteria.andRegIdEqualTo(emailReturn.getRegId());
            criteria.andRecordIdEqualTo(emailReturn.getRecordId());
        } else {
            return mapper.insertSelective(emailReturn) > 0;
        }
        emailReturn.setId(null);
        emailReturn.setRecordId(null); emailReturn.setRegId(null);
        return mapper.updateByExampleSelective(emailReturn, example) > 0;
    }

    @Transactional
    public boolean addReSendNum(int rtid) {
        return mapper.addReSendNum(rtid) > 0;
    }

    public EmailReturn findOne(int id) {
        return mapper.selectByPrimaryKey(id);
    }

    /**
     * 分页条件查询
     * @param pageNo 页码
     * @param size 分页尺寸
     * @param emailReturn 查询等值条件
     * @return 数据或null
     */
    public Page<EmailReturn> page(int pageNo, int size, EmailReturn emailReturn) {
        if (pageNo < 1) pageNo = 1;
        EmailReturnExample example = new EmailReturnExample();
        example.setOffset((pageNo - 1) * size);
        example.setLimit(size);
        example.setOrderByClause("id desc");
        if (emailReturn != null) {
            EmailReturnExample.Criteria criteria = example.createCriteria();
            if (emailReturn.getRegId() != null && emailReturn.getRegId() > 0) {
                criteria.andRegIdEqualTo(emailReturn.getRegId());
            }
            if (emailReturn.getRecordId() != null && emailReturn.getRecordId() > 0) {
                criteria.andRecordIdEqualTo(emailReturn.getRecordId());
            }
        }
        List<EmailReturn> emailReturns = mapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(emailReturns)) {
            long total = mapper.countByExample(example);
            return PageUtils.page(emailReturns, pageNo, size, total);
        }
        return null;
    }
}
